package com.jw.blog.wblog.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jw.blog.wblog.entity.Links;
import com.jw.blog.wblog.entity.custom.ResultBean;
import com.jw.blog.wblog.service.ILinksService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 无良芳
 * @since 2020-06-17
 */
@RestController
public class LinksController {
    @Resource
    private ILinksService iLinksService;

    @GetMapping("/selectAllLinks")
    public ResultBean<List<Links>> selectAllLinks(){
        QueryWrapper<Links> linksQueryWrapper = new QueryWrapper<>();
        linksQueryWrapper.lambda().eq(Links::getStatus,1).eq(Links::getIsApply,1);
        List<Links> links = iLinksService.list(linksQueryWrapper);
        ResultBean<List<Links>> resultBean = new ResultBean<>();
        resultBean.setData(links);
        return resultBean;
    }

    @PostMapping("/addLinks")
    public ResultBean<Links> addLinks(Links links,String prarm){

        links.setAddTime(LocalDateTime.now());
        ResultBean<Links> resultBean = new ResultBean<>();
        if (iLinksService.save(links)) {
            resultBean.setMsg("添加成功");
        }else {
            resultBean.setCode(ResultBean.FAIL);
        }
       return resultBean;
    }

    @PostMapping("/selectLinksById")
    public ResultBean<Object> selectLinksById(@RequestParam(value = "id") Integer id){
        Links links = iLinksService.getById(id);
        ResultBean<Object> resultBean = new ResultBean<>();
        if (links != null) {
            //这里已经实现了呢
            links.setClickNum(links.getClickNum() + 1);
            iLinksService.updateById(links);
        }else {
            resultBean.setCode(ResultBean.FAIL);
        }
        return resultBean;
    }
}
